package com.github.zzay.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.zzay.care.dto.VolunteerDto;
import com.github.zzay.care.entity.Elderly;
import com.github.zzay.care.entity.Volunteer;
import com.github.zzay.care.mapper.VolunteerMapper;
import com.github.zzay.care.service.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className VolunteerServiceImpl
 * @description Implementation class for "ElderlyService"
 * @create 2022/07/07 00:09
 */
@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {

    /**
     * Volunteer Service.
     */
    @Autowired
    private VolunteerMapper volunteerMapper;

    /**
     * Delete a volunteer's information.
     *
     * @param idCard ID card number
     * @return Operation result
     */
    public Boolean deleteInfo(String idCard) {
        Map<String, Object> map = new HashMap<>();
        VolunteerDto volunteer = volunteerMapper.selectByIdCard(idCard);
        if (volunteer == null) {
            return false;
        }
        map.put("id_card", idCard);
        volunteerMapper.deleteByMap(map);
        return true;
    }


    /**
     * Update a volunteer's information.
     *
     * @param volunteerDto Volunteer Dto
     * @return Operation result
     */
    public Boolean updateInfo(VolunteerDto volunteerDto) {
        VolunteerDto volunteer = volunteerMapper.selectByIdCard(volunteerDto.getIdCard());
        if (volunteer == null) {
            return false;
        }
        LambdaUpdateWrapper<Volunteer> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Volunteer::getIdCard, volunteer.getIdCard())
                .set(Volunteer::getSex, volunteerDto.getSex())
                .set(Volunteer::getPhone, volunteerDto.getPhone())
                .set(Volunteer::getBirthday, volunteerDto.getBirthday())
                .set(Volunteer::getCheckInDate, volunteerDto.getCheckInDate())
                .set(Volunteer::getCheckOutDate, volunteerDto.getCheckOutDate());
//        volunteer.setSex(volunteerDto.getSex());
//        volunteer.setPhone(volunteerDto.getPhone());
//        volunteer.setBirthday(volunteerDto.getBirthday());
//        volunteer.setCheckInDate(volunteerDto.getCheckInDate());
//        volunteer.setCheckOutDate(volunteerDto.getCheckOutDate());
//        volunteer.setProfilePhoto(volunteerDto.getProfilePhoto());
//        volunteer.setImageDir(volunteerDto.getImageDir());
        return volunteerMapper.update(null, lambdaUpdateWrapper) != 0;
    }

    /**
     * Get all information.
     *
     * @return A list of all information
     */
    public List<VolunteerDto> getAll() {
        return volunteerMapper.selectAll();
    }

    /**
     * Get a volunteer's information with the given id card number.
     *
     * @param idCard ID card number
     * @return Volunteer's information
     */
    public VolunteerDto getInfoById(String idCard) {
        return volunteerMapper.selectByIdCard(idCard);
    }

    /**
     * Get a volunteer's information with the given name.
     *
     * @param name Name
     * @return Volunteer's information
     */
    public VolunteerDto getInfoByName(String name) {
        return volunteerMapper.selectByName(name);
    }

}
